package and.example.adapter;

import and.example.addresscontacts.R;
import and.example.bitmap.AsyncDrawable;
import and.example.bitmap.BitmapWorkerTask;
import and.example.bitmap.BitmapWorkerTaskRes;
import and.example.interfaces.IArguments;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import and.example.bitmap.LoadBitmapAsync;

@SuppressWarnings("deprecation")
public class ContactAdapter extends CursorAdapter implements IArguments,
		LoadBitmapAsync.OnLoadBitmapHandle {

	Context context;
	public static LruCache<String, Bitmap> mMemoryCache;

	public ContactAdapter(Context context, Cursor c) {
		super(context, c);
		this.context = context;

	}

	public static void addBitmapToMemoryCache(String key, Bitmap bitmap) {

		if (getBitmapFromMemCache(key) == null) {
			mMemoryCache.put(key, bitmap);
		}
	}

	public static Bitmap getBitmapFromMemCache(String key) {
		return mMemoryCache.get(key);
	}

	@Override
	public void bindView(View arg0, Context arg1, Cursor arg2) {

		ViewHolder viewHolder = (ViewHolder) arg0.getTag();

		viewHolder.tv.setText(arg2.getString(arg2.getColumnIndex(COLUMNS[2])));
		viewHolder.tv1.setText(arg2.getString(arg2.getColumnIndex(COLUMNS[3])));

		String img = arg2.getString(arg2.getColumnIndex(COLUMNS[7]));

		// loadBitmap(img, viewHolder.iv);
		// loadBitmapInBackground(img, viewHolder);

		loadBitmap(img, viewHolder);

	}

	@Override
	public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {

		LayoutInflater sinh = LayoutInflater.from(arg0);
		View rowView = sinh.inflate(R.layout.contact_list_item, null);

		ViewHolder viewHolder = new ViewHolder();

		viewHolder.tv = (TextView) rowView.findViewById(R.id.contactTextView);
		viewHolder.tv1 = (TextView) rowView.findViewById(R.id.phoneView);
		viewHolder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
		viewHolder.iv1 = (ImageView) rowView.findViewById(R.id.i_more);

		rowView.setTag(viewHolder);
		return rowView;
	}

	public void loadBitmap(String imagePath, ViewHolder holder) {
		LoadBitmapAsync.loadBitmap(context, this, holder, imagePath);
	}

	public void loadBitmap(String pathImage, ImageView imageView) {

		if (BitmapWorkerTask.cancelPotentialWork(pathImage, imageView)) {

			final BitmapWorkerTask task = new BitmapWorkerTask(imageView,
					this.context);

			final AsyncDrawable asyncDrawable = new AsyncDrawable(
					this.context.getResources(), null, task);

			imageView.setImageDrawable(asyncDrawable);
			task.execute(pathImage);
		}
	}

	public void loadBitmap(int resId, ImageView imageView) {

		if (BitmapWorkerTaskRes.cancelPotentialWork(resId, imageView)) {

			final BitmapWorkerTaskRes task = new BitmapWorkerTaskRes(imageView);
			final BitmapWorkerTaskRes.AsyncDrawable asyncDrawable = new BitmapWorkerTaskRes.AsyncDrawable(
					context.getResources(), null, task);

			imageView.setImageDrawable(asyncDrawable);
			task.execute(resId);
		}
	}

	@Override
	public Bitmap loadBitmapInBackground(Object... strings) {
		String pathImage = strings[0].toString();
		return LoadBitmapAsync.decodeSampledBitmapFromFile(
				context.getResources(), pathImage, 100, 100);
	}

}
